package xin.mashibo.pattern.proxy;

import java.lang.reflect.Proxy;

/**
 * @author MaShibo
 * 日期 :  2019/1/29
 */
public class Main {

    public static void main(String[] args) {
        Printable p = new PrinterProxy("Alice");
        System.out.println("现在的名字是 " + p.getPrinterName() + "。");
        p.setPrinterName("Bob");
        System.out.println("现在的名字是 " + p.getPrinterName() + "。");
        p.print("Hello, world.");

        Printer printer = new Printer("被代理");
        Object proxyInstance = Proxy.newProxyInstance(printer.getClass().getClassLoader(), printer.getClass().getInterfaces(), (proxy, method, args1) -> {
            System.out.println("调用方法之前");
            return method.invoke(printer, args1);
        });
        ((Printable) proxyInstance).print("测试");
        System.out.println(proxyInstance);
    }

}
